package it.osg.openstory.web;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import it.osg.openstory.model.User;
import it.osg.openstory.services.AuthenticationService;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport implements SessionAware {
	private AuthenticationService authenticationService;
	private String mail;
	private String password;
	private Map sessionMap;
	private User user;

	public void setAuthenticationService(AuthenticationService authenticationService) {
		this.authenticationService = authenticationService;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		try {
			User user = authenticationService.authenticate(mail, password);
			this.user = user;
			if(user != null && !user.isActive())
				return INPUT;
			sessionMap.put("user", user);
			return SUCCESS;
		}
		catch (Exception exception) {
			addActionError("Non puoi entrare");
			return INPUT;
		}
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public void setSession(Map sessionMap) {
		this.sessionMap = sessionMap;
	}
	
	
}
